import datastructure.linklist.ListNode;

/**
 * @author ZCR
 * @create 2025/9/7 15:05
 */
public class Solution328 {
    public ListNode oddEvenList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode odd = new ListNode(0);
        ListNode even =new ListNode(0);
        ListNode p =head;
        ListNode pOdd = odd;
        ListNode pEven = even;
        int index = 1;
        while (p != null) {
            if (index % 2 == 0) {
                pEven.next = p;
                pEven = pEven.next;
            }else {
                pOdd.next = p;
                pOdd = pOdd.next;
            }
            p  = p.next;
            index++;
        }
        pOdd.next = even.next;
        pEven.next = null;
        return odd.next;
    }
}
